package com.gitee.xzb617.mybatis.query;

import com.gitee.xzb617.mybatis.query.condition.QueryCondition;
import com.gitee.xzb617.mybatis.query.util.SqlUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class CommonQueryImpl implements CommonQuery {


    private final JdbcTemplate jdbcTemplate;
    private String dbType;

    public CommonQueryImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
        this.dbType = SqlUtil.getDbType(this.jdbcTemplate.getDataSource());
    }


    @Override
    public List<Map<String, Object>> select(String tableName) {
        String sql = SqlUtil.generateFullSql(tableName, new String[]{}, null, dbType);
        return this.jdbcTemplate.queryForList(sql);
    }

    @Override
    public <T> List<T> select(String tableName, Class<T> clazz) {
        String sql = SqlUtil.generateFullSql(tableName, new String[]{}, null, dbType);
        return this.jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(clazz));
    }

    @Override
    public Map<String, Object> selectOne(String tableName) {
        String sql = SqlUtil.generateFullSql(tableName, new String[]{}, null, dbType);
        return this.jdbcTemplate.queryForMap(sql);
    }

    @Override
    public <T> T selectOne(String tableName, Class<T> clazz) {
        String sql = SqlUtil.generateFullSql(tableName, new String[]{}, null, dbType);
        return this.jdbcTemplate.queryForObject(sql, BeanPropertyRowMapper.newInstance(clazz));
    }

    @Override
    public List<Map<String, Object>> select(String tableName, String[] columns) {
        String sql = SqlUtil.generateFullSql(tableName, columns, null, dbType);
        return this.jdbcTemplate.queryForList(sql);
    }

    @Override
    public <T> List<T> select(String tableName, String[] columns, Class<T> clazz) {
        String sql = SqlUtil.generateFullSql(tableName, columns, null, dbType);
        return this.jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(clazz));
    }

    @Override
    public Map<String, Object> selectOne(String tableName, String[] columns) {
        String sql = SqlUtil.generateFullSql(tableName, columns, null, dbType);
        return this.jdbcTemplate.queryForMap(sql);
    }

    @Override
    public <T> T selectOne(String tableName, String[] columns, Class<T> clazz) {
        String sql = SqlUtil.generateFullSql(tableName, columns, null, dbType);
        return this.jdbcTemplate.queryForObject(sql, BeanPropertyRowMapper.newInstance(clazz));
    }

    @Override
    public List<Map<String, Object>> select(String tableName, String[] columns, QueryCondition conditions) {
        String sql = SqlUtil.generateFullSql(tableName, columns, conditions, dbType);
        return this.jdbcTemplate.queryForList(sql);
    }

    @Override
    public <T> List<T> select(String tableName, String[] columns, QueryCondition conditions, Class<T> clazz) {
        String sql = SqlUtil.generateFullSql(tableName, columns, conditions, dbType);
        return this.jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(clazz));
    }

    @Override
    public Map<String, Object> selectOne(String tableName, String[] columns, QueryCondition conditions) {
        String sql = SqlUtil.generateFullSql(tableName, columns, conditions, dbType);
        return this.jdbcTemplate.queryForMap(sql);
    }

    @Override
    public <T> T selectOne(String tableName, String[] columns, QueryCondition conditions, Class<T> clazz) {
        String sql = SqlUtil.generateFullSql(tableName, columns, conditions, dbType);
        return this.jdbcTemplate.queryForObject(sql, BeanPropertyRowMapper.newInstance(clazz));
    }

}
